﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class AddRoute : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void saveRoute()
        {
            string routeTitle = this.GetParamHtValue("routeTitle");
            if (string.IsNullOrEmpty(routeTitle))
            {
                this.ExecJScript(@"layer.alert('标题不能为空.', 3);");
                return;
            }
            string coverageArea = this.GetParamHtValue("coverageArea");
            if (string.IsNullOrEmpty(coverageArea))
            {
                this.ExecJScript(@"layer.alert('请填写涵盖景点.', 3);");
                return;
            }
            string routeJianjie = this.GetParamHtValue("routeJianjie");
            if (string.IsNullOrEmpty(routeJianjie))
            {
                this.ExecJScript(@"layer.alert('请填写路线介绍.', 3);");
                return;
            }

            string scenicSpots = this.GetParamHtValue("scenicSpots");
            if (string.IsNullOrEmpty(scenicSpots))
            {
                this.ExecJScript(@"layer.alert('请填写涵盖景点.', 3);");
                return;
            }
            string characteristic = this.GetParamHtValue("characteristic");
            if (string.IsNullOrEmpty(characteristic))
            {
                this.ExecJScript(@"layer.alert('请填写路线特色.', 3);");
                return;
            }

            string txtInfo = this.GetParamHtValue("txtInfo");
            if (string.IsNullOrEmpty(txtInfo))
            {
                this.ExecJScript(@"layer.alert('请填写简介.', 3);");
                return;
            }

            string countryId = this.GetParamHtValue("countryId");

            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("destinationId");
            if (cityId == "0" || countryId == "0")
            {
                this.ExecJScript(@"layer.alert('请选择地区.', 3);");
            }

            string routePrice = this.GetParamHtValue("routePrice");
            if (string.IsNullOrEmpty(routePrice))
            {
                this.ExecJScript(@"layer.alert('请填写价格.', 3);");
                return;
            }
            string djackList = this.GetParamHtValue("djackList");
            string shoutu = this.GetParamHtValue("shoutu");
            if (string.IsNullOrEmpty(shoutu))
            {
                this.ExecJScript(@"layer.alert('请选择首图.', 3);");
                return;
            }

            //地接客
            List<string> djkList = new List<string>();
            if (!string.IsNullOrEmpty(djackList))
            {
                string[] array = djackList.Split(',');
                if (array.Length > 0)
                {
                    djkList = array.ToList();
                }
            }
            using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
            {
                //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                shoutu = oCBizSysFile.SaveImgToServer("", msBsc.SvRef.UserInfo.Operator, "1", "VEH", ""
                    , shoutu//文件来源虚拟路径
                    , ""//文件来源物理路径，虚拟和物理只有一个就行
                    , true);
            }

            using (CBizRoute route = new CBizRoute())
            {
                string[] routeMessage = route.AddRoute(routeTitle, txtInfo, characteristic, countryId, cityId, destId, scenicSpots, coverageArea, shoutu, routeJianjie, routePrice, djkList.Count > 0 ? 1: 0);
                if (Convert.ToBoolean(routeMessage[0]) && djkList.Count < 1)
                {
                    this.ExecJScript("success();");
                }
                else if (djkList.Count > 0 && Convert.ToBoolean(routeMessage[0]))
                {
                    using (CBizAdditionalInfo addInfo = new CBizAdditionalInfo())
                    {
                        string message = addInfo.AddItionalInfo(djkList, routeMessage[1]);
                        if (message == "OK")
                        {
                            this.ExecJScript("success();"); 
                        }
                        else
                        {
                            this.ExecJScript("layer.alert('" + message + ".', 3);");
                        }
                    }
                }
                else
                {
                    this.ExecJScript(@"layer.alert('" + routeMessage[1] + ".', 3);");
                }
            }

        }


        //获取地接客
        public void GetUserAcction()
        {
            string djkAccunt = this.GetParamHtValue("djkAcction");

            if (string.IsNullOrEmpty(djkAccunt))
            {
                this.ExecJScript("layer.alert('地接客帐号不能为空.', 3);");
                return;
            }
            using (CBizCustomerRegister register = new CBizCustomerRegister())
            {
                DataTable dt = register.GetDjkUserInfo(djkAccunt);
                if (dt == null)
                {
                    this.ExecJScript("$('#djkAccunt').html('<li>没有找到该地接客</li>');");
                    return;
                }
                var djkList = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 20, "");

                this.ExecJScript("loadDjkInfo(" + djkList + ");");
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "saveRoute")
            {
                saveRoute();
            }
            else if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
            else if (this.strAction == "getUser")
            {
                GetUserAcction();
            }
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}